//
//  YHistoryInfoViewController.m
//  shijianpinggu
//
//  Created by apple on 2017/3/7.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YHistoryInfoViewController.h"

@interface YHistoryInfoViewController ()<UIWebViewDelegate>
@property (retain, nonatomic) IBOutlet UIWebView *protWebView;
@end

@implementation YHistoryInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",self.webId);
    self.protWebView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:self.protWebView];
    [self loadDataFormUrl];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//请求数据
-(void)loadDataFormUrl{
    
    
    NSDictionary *parameter = @{@"site_id":self.webId,@"startime":self.starTime,@"endtime":self.endTime};
    
    //    NSLog(@"%@",dict);
    
    // 初始化Manager
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 不加上这句话，会报“Request failed: unacceptable content-type: text/plain”错误，因为我们要获取text/plain类型数据
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    // post请求
    [manager POST:@"http://wangan.ldynet.cn/index.php/IndexAppHistory/history_detail" parameters:parameter constructingBodyWithBlock:^(id  _Nonnull formData) {
        // 拼接data到请求体，这个block的参数是遵守AFMultipartFormData协议的。
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        // 这里可以获取到目前的数据请求的进度
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        // 请求成功，解析数据
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSLog(@"11111111111111%@", dic);
        if(dic == nil){
            [self alertWithString:@"请稍后再试"];
        }else{
        NSURL *url = [[NSURL alloc]initWithString:dic[@"url"]];
        
        [self.protWebView loadRequest:[NSURLRequest requestWithURL:url]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // 请求失败
        // [self alertWithString:@"请稍后再试"];
    }];
    
    
    //[self loginUrl];
}

//几个代理方法

- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    NSLog(@"webViewDidStartLoad");
    
}

- (void)webViewDidFinishLoad:(UIWebView *)web{
    
    NSLog(@"webViewDidFinishLoad");
    
}

-(void)webView:(UIWebView*)webView  DidFailLoadWithError:(NSError*)error{
    
    NSLog(@"DidFailLoadWithError");
    
}
-(void)alertWithString:(NSString *)string{
    
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:string preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"The \"Okay/Cancel\" alert's other action occured.");
    }];
    [alertC addAction:otherAction];
    [self showViewController:alertC sender:nil];
    //    for (int i=0; i<self.navigationController.viewControllers.count; i++) {
    //        if ((self.navigationController.viewControllers[i] == self)) {
    //            [self.navigationController presentViewController:alertC animated:YES completion:nil];
    //        }
    //
    //    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
